package com.uphone.magicleafteacher.pro.activity;

import android.arch.lifecycle.LifecycleOwner;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.radish.baselibrary.Intent.IntentData;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RequestBodyUtil;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.pro.adapter.CourseLiveStudentAdapter;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.ClassStudentBean;
import com.uphone.magicleafteacher.vo.CourseLiveListEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;

public class CourseLiveStudentListActivity extends BaseGActivity {
    @BindView(R.id.img_f)
    ImageView imgF;
    @BindView(R.id.tool)
    Toolbar tool;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.button)
    Button button;
    private SharedPreferences preferences;
    private String token;
    @IntentData
    private int liveStatus;
    @IntentData
    private int id;
    private CourseLiveStudentAdapter courseLiveStudentAdapter;

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_course_live_student;
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
        preferences = getSharedPreferences("moye", MODE_PRIVATE);
        token = preferences.getString("token", "");

        if (liveStatus != 3) {
            button.setVisibility(View.VISIBLE);
        } else {
            button.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("liveId", id + "");
        MyApp.apiService(ApiService.class)
                .liveClassStudent(map, map1)
                .compose(RxSchedulers.io_main())
                .subscribe(bean -> {
                    if (CourseLiveStudentListActivity.this != null)
                        CourseLiveStudentListActivity.this.closeLoading();
                    if (bean != null) {
                        try {
                            String jsonStr = bean.string();
                            Gson gson = GsonUtils.getGson();
                            ClassStudentBean classStudentBean = gson.fromJson(jsonStr, ClassStudentBean.class);
                            courseLiveStudentAdapter = new CourseLiveStudentAdapter(R.layout.item_course_live_student, classStudentBean.getStuList(), liveStatus);
                            recyclerview.setAdapter(courseLiveStudentAdapter);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, throwable -> {

                    CourseLiveStudentListActivity.this.closeLoading();
                    CourseLiveStudentListActivity.this.onFail();
                });
    }

    @Override
    protected void initListener() {

    }

    class LiveStudentBean {
        int liveId;
        int jifen;
        int studentId;

        public LiveStudentBean(int liveId, int jifen, int studentId) {
            this.liveId = liveId;
            this.jifen = jifen;
            this.studentId = studentId;
        }
    }

    @OnClick({R.id.img_f, R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_f:
                finish();
                break;
            case R.id.button:
                if (courseLiveStudentAdapter == null) {
                    ToastUtils.showShort("页面正在加载中。。。");
                    return;
                }
                List<LiveStudentBean> list = new ArrayList<>();
                for (int i = 0; i < courseLiveStudentAdapter.getData().size(); i++) {
                    if (TextUtils.isEmpty(courseLiveStudentAdapter.getItem(i).getJifen())) {
                        ToastUtils.showShort("请填写积分值");
                        return;
                    }
                    String jifen = courseLiveStudentAdapter.getItem(i).getJifen();
                    int jifenInt = Integer.parseInt(jifen);
                    list.add(new LiveStudentBean(id, jifenInt, courseLiveStudentAdapter.getItem(i).getStudentId()));
                }
                String s = GsonUtils.getGson().toJson(list);
                LogUtils.e("参数：" + s);
                MyApp.apiService(ApiService.class)
                        .setStudentJifen(s + "")
                        .compose(RxSchedulers.io_main())
                        .doOnSubscribe(d -> {
                            showLoading();
                        })
                        .doFinally(() -> {
                            closeLoading();
                        })
                        .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) this)))
                        .subscribe(new SuccessfulConsumer() {
                            @Override
                            public void success(String jsonObject) {
                                EventBus.getDefault().post(new CourseLiveListEvent());
                                CommonUtil.quitActivity(CourseLiveStudentListActivity.this, "评分成功");
                            }
                        }, new FailureConsumer());
                break;
        }
    }
//    @Override
//    protected Observable<ResponseBody> loadData(ApiService apiService) {
//        mAdapter.setNewData(CommonUtil.getList(10));
//        return null;
//    }
//
//    @Override
//    protected List<Object> getDataList(String json) {
//        return null;
//    }
//
//    @Override
//    protected void itemClick(CourseLiveStudentAdapter adapter, View view, int position) {
//    }
//
//    @Override
//    protected CourseLiveStudentAdapter initAdapter() {
//        return new CourseLiveStudentAdapter();
//    }
//
//    @Override
//    protected void initBundle() {
//
//    }
//
//    @Override
//    protected void initTitle() {
//        initTitle("参与学员");
//    }
}
